//
//  RefreshHeaderAnimator.swift
//  ESPullToRefreshExample
//

import UIKit
import YYKit
import ESPullToRefresh

public class RefreshHeaderAnimator: UIView, ESRefreshProtocol, ESRefreshAnimatorProtocol {
    
    public let loadingDescription: String = "刷新中"
    public let pullDescription: String    = "下拉刷新"
    public let releaseDescription: String = "松开刷新"
    
    public var insets: UIEdgeInsets = UIEdgeInsets.zero
    public var view: UIView { return self }
    public var duration: TimeInterval = 0.3
    public var trigger: CGFloat = 70.0
    public var executeIncremental: CGFloat = 70.0
    public var state: ESRefreshViewState = .pullToRefresh

    public var imgLoading: YYImage? {
        didSet {
            imageView.image = imgLoading
            imageView.stopAnimating()
        }
    }
    
    private let imageView: YYAnimatedImageView = {
        let imageView = YYAnimatedImageView.init()
        
        let path = RouTools.bundlePath("loading", "gif", "Rou.bundle")
        imageView.image = YYImage(contentsOfFile: path)
        imageView.stopAnimating()
        return imageView
    }()
    
    private let labInfo: UILabel = {
        let labInfo = UILabel()
        labInfo.textColor = UIColor.black
        labInfo.font = UIFont.systemFont(ofSize: 10)
        labInfo.textAlignment = .center
        return labInfo
    }()
    
    private let viewContent: UIView = {
        let viewContent = UIView()
        viewContent.backgroundColor = UIColor.clear
        return viewContent
    }()
    
    override public init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.clear
        
        addSubview(viewContent)
        viewContent.snp.makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        viewContent.addSubview(imageView)
        imageView.snp.makeConstraints { (make) in
            make.top.left.right.equalTo(UIEdgeInsetsMake(10, 20, 0, 20))
            make.centerX.equalTo(viewContent)
//            make.size.equalTo(CGSize.init(width: 16, height: 16))
        }
        
        viewContent.addSubview(labInfo)
        labInfo.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(UIEdgeInsetsMake(0, 0, 10, 0))
            make.top.equalTo(imageView.snp.bottom).offset(10)
        }
    }
    
    public required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public func refreshAnimationBegin(view: ESRefreshComponent) {
        self.imageView.startAnimating()
        labInfo.text = loadingDescription
    }
    
    public func refreshAnimationEnd(view: ESRefreshComponent) {
        imageView.stopAnimating()
        labInfo.text = pullDescription
    }
    
    public func refresh(view: ESRefreshComponent, progressDidChange progress: CGFloat) {}
    
    public func refresh(view: ESRefreshComponent, stateDidChange state: ESRefreshViewState) {
        guard self.state != state else {
            return
        }
        
        self.state = state
        switch state {
        case .pullToRefresh:
            labInfo.text = pullDescription
            imageView.startAnimating()
            break
        case .releaseToRefresh:
            labInfo.text = releaseDescription
            break
        case .refreshing:
            labInfo.text = loadingDescription
            imageView.startAnimating()
            break
        default:
            break
        }
    }
    
    public override func draw(_ rect: CGRect) {
        super.draw(rect)
        
    }
    
}
